<template>
  <div>
    <div class="permissManaTool">
      <el-input placeholder="请输入角色英文名" size="small" v-model="role.name" >
        <template slot="prepend">ROLE_</template>
      </el-input>
      <el-input placeholder="请输入角色中文名" size="small" v-model="role.nameZh" ></el-input>
      <el-button type="primary" size="small" @click="addRole" style="margin-left: 10px">添加</el-button>
    </div>
    <!-- 手风琴菜单开始 -->
    <div>
      <el-collapse class="permissManamain" accordion @change="change" v-model="activeName">
        <!-- 这里动态绑定了：name 所以在@change="change"  这个回调函数中可以作为rid为参数传递进来   -->
        <el-collapse-item :title="r.nameZh" :name="r.id" v-for="(r,index) in roles" :key="index">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>可访问的资源</span>
              <el-button style="float: right; padding: 3px 0; color: red" type="text" icon="el-icon-delete" @click="deleteRole(r)">删除</el-button>
            </div>
            <div>
              <!-- 树形控件 下面这个ref="tree" 相当于给没一个树型控件  注意这个key绑定了不同的key 防止Vue认为是同一个 树形组件-->
              <el-tree
                       :key="index"
                       node-key="id"
                       :default-checked-keys="selectMenus"
                       show-checkbox
                       :data="allmenus"
                       :props="defaultProps"
                       ref="tree"
              ></el-tree>
              <div style="display: flex;justify-content: flex-end">
                <el-button size="small" @click="cancelUpdate">取消修改</el-button>
                <el-button size="small" @click="doUpdate(r.id,index)">确认修改</el-button>
              </div>
            </div>
          </el-card>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<script>
export default {
  name: "PermissMana",
  data() {
    return {
      role:{
        name:'',
        nameZh:''
      },
      activeName: '-1',//默认情况下面板为关闭状态
      roles:[],//角色
      allmenus:[],//所有菜单
      defaultProps: {
        children: 'children',//树控件中的值所对应的，后台数据中“子树“的名字
        label: 'name'//树控件中的值所对应的，后台数据的名字
      },
      selectMenus:[]  //默认选中的数据
    }
  },
  beforeMount() {
    this.initRoles()
  },
  methods:{
    cancelUpdate(){
      this.activeName = -1;
    },
    doUpdate(rid,index){
      //使用$ref拿到对应子组件中的数组，然后通过index 获取指定的一个"tree" 来做更新操作
      let tree= this.$refs.tree[index];
      let selectKey = tree.getCheckedKeys(true);// 参数为布尔值，若为true只选择叶子节点的值
      //发送请求之前格式化数据，
      let url ='/system/basic/permiss/?rid='+rid;
      //遍历已经选中的数组
      selectKey.forEach(item => {
          url += "&mids=" + item;
      })
      //发请求
      this.putRequest(url).then(resp => {
        if(resp){
          this.activeName= -1;
        }
      })
    },

    //初始化选中的数据 通过需要展开的Id来拿到对于的权限
     InitSelectMenus(rid){
      this.getRequest("/system/basic/permiss/mids/"+rid).then(resp => {
        if(resp){
          this.selectMenus = resp;
        }
      }
     )
    },

    change(rid){
      console.log(rid)
      //打开时去对应的菜单赋值
      if(rid){
        this.initAllMenus()
        this.InitSelectMenus(rid)
      }
    },

    //初始化所有菜单
    initAllMenus(){
      this.getRequest("/system/basic/permiss/menus").then(resp => {
        if(resp){
          //赋值给对应的菜单
          this.allmenus = resp;
        }
      })
    },

    //初始化所有角色
    initRoles(){
      this.getRequest("/system/basic/permiss/").then( resp => {
        if(resp){
          this.roles = resp;
        }
      })
    },

    //添加角色
    addRole(){
        if(this.role.nameZh != "" && this.role.nameZh !=""){
          this.postRequest("/system/basic/permiss/role",this.role).then(resp => {
              if(resp){
                //添加成功后刷新角色信息
                this.initRoles()
                this.role.name="";
                this.role.nameZh="";
              }
          })
        } else {
          this.$message({
            message: "请完善输入框中的内容",
            type: "error"
          })
        }
    },

    //删除角色
    deleteRole(r){
      this.$confirm('确定要删除【'+r.nameZh+'】', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest("/system/basic/permiss/role/"+r.id).then(resp => {
          if (resp) {
            this.initRoles();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }

  }
}
</script>

<style>
  .permissManaTool{
    display: flex;
  }
  .permissManaTool .el-input{
    width: 300px;
    margin-right: 20px;
  }
  .permissManamain{
    margin-top: 15px;
    width: 700px;
  }
</style>
